package com.meitianhui.convenitenbuy.module.home.adapter;

import android.content.res.Resources;
import android.support.annotation.Nullable;
import android.text.Html;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.module.home.model.bean.HomeGoodsInfo;
import com.meitianhui.convenitenbuy.util.SpannableEditor;
import com.meitianhui.convenitenbuy.util.StringUtils;

import java.util.List;

/**
 * 0元购（咚咚抢） 页面 adapter.
 * Created by Liam on 2018/5/17.
 */
public class ZeroBuyAdapter extends BaseQuickAdapter<HomeGoodsInfo.Convient, BaseViewHolder> {

    private List<HomeGoodsInfo.Convient> list;
    private RequestOptions requestOptions = null;

    public ZeroBuyAdapter(@Nullable List<HomeGoodsInfo.Convient> data) {
        super(R.layout.item_zero_buy_grid,data);
        list = data;
    }

    @Override
    protected void convert(BaseViewHolder helper, HomeGoodsInfo.Convient item) {
        ImageView ivImage = helper.getView(R.id.iv_zero_buy_img);
        TextView tvTitle = helper.getView(R.id.tv_zero_buy_title);
        TextView tvTaoBaoPrice = helper.getView(R.id.tv_zero_buy_taobao_price);
        TextView tvSoldCount = helper.getView(R.id.tv_zero_buy_sold_count);
        TextView tvSavedCash = helper.getView(R.id.tv_zero_buy_saved_cash);
        TextView tvBuyPrice = helper.getView(R.id.tv_zero_buy_price);

        Resources resources = mContext.getResources();
        // title.
        helper.setText(R.id.tv_zero_buy_title, item.getTitle());
        // taoBao price.
        String taoBaoPrice = resources.getString(R.string.show_goods_taobao_price);
        taoBaoPrice = String.format(taoBaoPrice, item.getTaobao_price());
        tvTaoBaoPrice.setText(new SpannableEditor(mContext, taoBaoPrice)
                .setTextStrikethrough(taoBaoPrice).build());
        // has sold out.
        String hasSoldOutCount = resources.getString(R.string.show_goods_has_sold_number);
        hasSoldOutCount = String.format(hasSoldOutCount, item.getSale_qty());
        tvSoldCount.setText(Html.fromHtml(hasSoldOutCount));
        // saved money.
        String savedCash = resources.getString(R.string.show_goods_saved_money);
        String money = StringUtils.moneySub(item.getMarket_price(), item.getDiscount_price());
        tvSavedCash.setText(String.format(savedCash, money));
        // buy price : 到手价
        String buyPrice = resources.getString(R.string.show_goods_buy_price);
        buyPrice = String.format(buyPrice, item.getDiscount_price());
        tvBuyPrice.setText(Html.fromHtml(buyPrice));

        if (requestOptions == null) {
            requestOptions = new RequestOptions().dontAnimate()
                    .override(250, 250)
                    .centerCrop()
                    .skipMemoryCache(false)
                    .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                    .placeholder(R.mipmap.icon_default_picture_store)
                    .error(R.mipmap.icon_default_picture_store);
        }
        Glide.with(mContext)
                .load(item.getPic_info_url()).apply(requestOptions).thumbnail(0.1f).into(ivImage);
    }

}
